package com.jgy.utils;

import javax.servlet.http.HttpServletRequest;

/**
 * @auther zcy
 * @data 2020/6/22 17:14
 */
public class IPUtil {


    /**
     * 获取请求ip
     *
     * @param request
     * @return
     */
    public static String getRemotIP(HttpServletRequest request) {

        String ip = request.getHeader("x-forwarded-for");

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

            ip = request.getHeader("Proxy-Client-IP");

        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

            ip = request.getHeader("WL-Proxy-Client-IP");

        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

            ip = request.getRemoteAddr();

        }

        final String[] arr = ip.split(",");

        for (final String str : arr) {

            if (!"unknown".equalsIgnoreCase(str)) {

                ip = str;

                break;

            }

        }

        return ip;

    }

}
